package com.ksd.api.controller;

import com.ksd.api.beans.CheckVersionResponse;
import com.ksd.api.utils.HttpServletUtil;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.system.domain.SysApkVersion;
import com.ksd.system.domain.SysBoard;
import com.ksd.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class checkVersion extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(checkVersion.class);
    @Autowired
    private ISysApkVersionService sysApkVersionService;
    @Autowired
    private ISysBoardService sysBoardService;

    @RequestMapping(value = "/api/user/version/check", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getVodList(HttpServletRequest httpServletRequest) {
        logger.info("===================版本升级===============");
        StringBuffer requestURL = httpServletRequest.getRequestURL();

        HttpServletUtil.getRequestParam(httpServletRequest);
        SysBoard sysBoard = sysBoardService.selectSysBoardById(SysBoard.serviceFileId);
        String serviceIpUrl = sysBoard.getUrl();

        CheckVersionResponse checkVersionResponse = new CheckVersionResponse();
        SysApkVersion sysApkVersion = new SysApkVersion();
        sysApkVersion.setType(1);
        List<SysApkVersion> sysApkVersions = sysApkVersionService.selectSysApkVersionList(sysApkVersion);
        logger.info("数据库查询到终端APK版本个数:"+sysApkVersions.size());
        //将最新的版本数据传递过去
        if (sysApkVersions != null && sysApkVersions.size() > 0) {
            SysApkVersion result = sysApkVersions.get(0);
            checkVersionResponse.setVersion(result.getVersion());
            checkVersionResponse.setUrl(serviceIpUrl+result.getUrl());
            logger.info("升级版本号:"+result.getVersion()+"==========升级包地址:"+result.getUrl());
            return AjaxResult.success(checkVersionResponse);
        }else{
            checkVersionResponse.setVersion(null);
            checkVersionResponse.setUrl(null);
            logger.info("未查询到终端APK可升级的版本");
            return AjaxResult.success(checkVersionResponse);
        }
    }

}